﻿package net.chrisjrock.loader.bulletloader {
	import flash.utils.Dictionary;
	
	/**
	 * ...
	 * @author Christopher J. Rock
	 */
	public class BulletPath {
		public static const DEFAULT_KEY:String = "default";
		public static const ABSOLUTE_KEY:String = "absolute";
		public static const VIDEO_KEY:String = "video";
		
		public static const ROOT_KEY_DELIMITER:String = ":";
		
		
		private static var _rootPath:String;
		private static var _videoRootPath:String;
		
		private static var _rootSuffix:String;
		
		private static var _rootPathByKey:Dictionary = new Dictionary();
		private static var _suffixByKey:Dictionary = new Dictionary();
		
		public static function parseKey (url:String):String
		{
			if (url != null)
			{
				var key:String = url.split(ROOT_KEY_DELIMITER)[0];
				if (key == DEFAULT_KEY || key == ABSOLUTE_KEY || key == VIDEO_KEY || _rootPathByKey[key] != null)
				{
					return key;
				}
			}
			return null;
		}
		
		public static function getRootPath (key:*):String
		{
			if (key == DEFAULT_KEY)
			{
				return _rootPath;
			}
			if (key == ABSOLUTE_KEY)
			{
				return "";
			}
			else if (key == VIDEO_KEY && _videoRootPath != null)
			{
				return _videoRootPath;
			}
			return _rootPathByKey[key];
		}
		
		public static function parseRootPath (key:String , asVideo:Boolean = false , rootKey:String = null):String
		{
			if (key != null)
			{
				var rootPath:String = getRootPath(key);
				if (rootPath == null && rootKey != null)
				{
					rootPath = getRootPath(rootKey);
				}
				
				if (rootPath != null)
				{
					return rootPath;
				}
			}
			
			if (asVideo && _videoRootPath != null)
			{
				return _videoRootPath;
			}
			
			return _rootPath != null ? _rootPath : "";
		}
		
		public static function getSuffix (key:*):String
		{
			if (key == DEFAULT_KEY)
			{
				return _rootSuffix;
			}
			if (key == ABSOLUTE_KEY)
			{
				return "";
			}
			return _suffixByKey[key];
		}
		
		public static function parseSuffix (key:String , rootKey:String = null):String
		{
			if (key != null)
			{
				var suffix:String = getSuffix(key);
				if (suffix == null && rootKey != null)
				{
					suffix = getSuffix(rootKey);
				}
				
				if (suffix != null)
				{
					return suffix;
				}
			}
			
			return _rootSuffix != null ? _rootSuffix : "";
		}
		
		public static function applyPath(url:String , asVideo:Boolean = false , rootKey:String = null):String
		{
			if (url != null)
			{
				var key:String = parseKey(url);
				if (key != null)
				{
					url = url.substr(key.length + 1);
				}
				url = parseRootPath(key , asVideo , rootKey) + url + parseSuffix(key , rootKey);
			}
			
			return url;
		}
		
		public static function addRootPath (rootPath:String , key:* , suffix:String = null):void
		{
			if (key != null && rootPath != null)
			{
				_rootPathByKey[key] = rootPath;
				if (suffix != null)
				{
					_suffixByKey[key] = suffix;
				}
			}
		}
		
		public static function forceNewRootPath (newRootPath:String):void
		{
			_rootPath = newRootPath;
		}
		
		public static function forceNewRootSuffix (newRootSuffix:String):String
		{
			return _rootSuffix = newRootSuffix;
		}
		
		public static function forceNewVideoPath (newVideoPath:String):String
		{
			return _videoRootPath = newVideoPath;
		}
		
		public static function get rootPath ():String
		{
			return _rootPath;
		}
		
		public static function set rootPath (value:String):void
		{
			if (_rootPath == null) _rootPath = value;
		}
		
		public static function get rootSuffix ():String
		{
			return _rootSuffix;
		}
		
		public static function set rootSuffix (value:String):void
		{
			if (_rootSuffix == null) _rootSuffix = value;
		}
		
		public static function get videoRootPath ():String
		{
			return _videoRootPath;
		}
		
		public static function set videoRootPath (value:String):void
		{
			if (_videoRootPath == null) _videoRootPath = value;
		}
		
	}
	
}